package com.cmcc.waf.report.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cmcc.waf.report.feign.model.AttackRegionTopVO;
import com.cmcc.waf.report.feign.model.ResponseVO;
import com.cmcc.waf.report.feign.model.WebSafeRequest;
import com.cmcc.waf.report.service.AttackRegionService;
import com.cmcc.waf.report.utils.EcloudResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * zhangheng
 * 攻击来源地区TOP5
 * 2025/1/16 11:09
 *  * 接口文档：<a href="https://ecloud.10086.cn/op-help-center/doc/article/90437">业务概况-web安全防护-攻击分布区域</a>
 */
@RestController
public class AttackRegionController {
    @Autowired
    private AttackRegionService attackRegionService;

    @PostMapping("/attack-region/top")
    public EcloudResult<List<AttackRegionTopVO>> attackRegionTop5(@RequestBody @Validated WebSafeRequest request) {
        request.setStartTime(StrUtil.isEmpty(request.getStartTime()) ? DateUtil.beginOfDay(DateUtil.date()).toString() : request.getStartTime());
        request.setEndTime(StrUtil.isEmpty(request.getEndTime()) ? DateUtil.now() : request.getEndTime());
        ResponseVO<List<AttackRegionTopVO>> response = attackRegionService.attackRegionTop5(request);
        if (response.success()) {
            return new EcloudResult<List<AttackRegionTopVO>>().ok(response.getBody());
        } else {
            return new EcloudResult<List<AttackRegionTopVO>>().error(Integer.parseInt(response.getCode()), response.getMessage());
        }
    }
}
